<template>
    <div>
        <Row>
            <i-col span="24">
                <Card>
                    <Button class="opt-btn" @click="confirmSelection" type="primary" v-if="optType === 'add'">确认选择</Button>
                    <Button class="opt-btn" @click="showAddModal" type="primary" v-if="optType === 'show'">新增</Button>
                    <Button class="opt-btn" @click="removeSelection" type="error" v-if="optType === 'show'">删除</Button>
                    <Button class="opt-btn" @click="showSearchModal" type="primary">高级搜索</Button>
                    <Tooltip class="opt-btn" content="刷新" placement="right">
                        <Button icon="md-refresh" type="success" shape="circle" @click="searchTable(false, false)"></Button>
                    </Tooltip>
                    <Tooltip class="opt-btn" content="重置搜索" placement="right">
                        <Button icon="md-repeat" type="success" shape="circle" @click="researchTable"></Button>
                    </Tooltip>
                    <GoodsBrandTableMultiple ref="table" v-on:searchTable="searchTable" v-on:showDetailModal="showDetailModal"/>
                </Card>
            </i-col>
        </Row>
        <GoodsBrandSearchModal ref="searchModal" v-on:searchTable="searchTable"/>
        <GoodsBrandDetailModal ref="detailModal"/>
    </div>
</template>

<script>
    import * as utils from '@/api/utils-v2'
    import GoodsBrandTableMultiple from './GoodsBrandTableMultiple.vue'
    import GoodsBrandSearchModal from './GoodsBrandSearchModal.vue'
    import GoodsBrandDetailModal from './GoodsBrandDetailModal.vue'

    import {batchSaveGoodsCategoryBrand} from '@/api/goods_category'
    import * as ResponseStatus from '@/api/response-status'
    export default {
        name: 'GoodsBrandMainMultiple',
        components: {
            GoodsBrandTableMultiple,
            GoodsBrandSearchModal,
            GoodsBrandDetailModal
        },
        props: {
            optType: {
                type: String,
                default: 'show'
            }
        },
        data() {
            return {
                selectedData: [],
                selectedDataIdProp: '',
                extraData: {}
             }
        },
        computed: {},
        mounted() {},
        methods: {
            searchTable(reset, clearOldData) {
                utils.search(this, reset, clearOldData)
            },
            researchTable() {
                this.$refs.searchModal.resetForm()
                setTimeout(() => {
                    utils.search(this, true)
                }, 10);
            },
            showDetailModal(row) {
                let detailModal = this.$refs.detailModal
                detailModal.modal.detail = true
                detailModal.form = row
				    detailModal.getDictItemsDisplayValue()
            },
            showSearchModal() {
                let searchModal = this.$refs.searchModal
                searchModal.modal.search = true
            },
           showAddModal() {
                this.$emit('showAddModal')
            },
            removeSelection() {
              if (this.$refs.table.table.selections.length === 0) {
                    this.$Message.warning('请选择需要删除的品牌')
                    return
                }
                this.$Modal.confirm({
                    title: '确认删除吗？',
                    content: '确认批量删除选择的数据吗？',
                    onOk: () => {
                        let brandIds = []
                        this.$refs.table.table.selections.forEach(item => {
                            brandIds.push(item.id)
                        })
                        utils.doPostJson('/goods-category-brand/admin/remove-brand/' + this.extraData.categoryId, brandIds, {}).then(res => {
                            const data = res.data
                            if (data.code === ResponseStatus.OK) {
                                this.$Message.info("删除品牌成功")
                                this.$refs.table.table.selections = []
                                this.$emit('refresh', this.extraData.categoryId)
                            } else {
                                this.$Message.error(data.message)
                            }
                        }).catch(err => {
                            this.$Message.error(err)
                        })
                    },
                    onCancel: () => {}
                })
            },                                         
            confirmSelection() {
              // 确认选择的逻辑
              if (this.$refs.table.table.selections.length === 0) {
                    this.$Message.warning('请选择需要增加的品牌')
                    return
                }
              let params = []
              this.$refs.table.table.selections.forEach(item => {
                params.push({
                  categoryId: this.extraData.categoryId,
                  brandId: item.id
                })
              })
                  
              batchSaveGoodsCategoryBrand(params).then(res => {
                const data = res.data
                if (data.code === ResponseStatus.OK) {
                  this.$Message.info("新增品牌成功")
                  this.$refs.table.table.selections = []
                        this.$emit('closeAddModal')
                        this.$emit('refresh', this.extraData.categoryId)
                } else {
                  this.$Message.error(data.message)
                }
              }).catch(err => {
                this.$Message.error(err)
              })
            },
        }
    }
</script>

<style>
</style>
